<?php
	session_start();
	require('include/inc.start.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Find books on getub</title>
<?php include ('common_head.html');?>
<link href="css/terms.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php include ('login.php');?>
<div class="back">	
  <div class="footerBg">
  <div id="frame">
  <?php include ('header.php');?>
  <div class="mainfrm">
  <div class="bluestripfrm_terms">
  <div class="blue_heading_terms">
  <h1>Buy books </h1>
  </div>
  </div><!--bluestripfrm-->
<div id="pagecontent_terms">
<?php
include("include/connectionParameters.php");
	
	if (isset($_POST['searchText'])){
		// we have a real search
		
		// saving post values
		$searchBy = $_POST['searchBy'];
		$searchText = $_POST['searchText'];
		
		$searchCondition = "";
		switch ($searchBy){
		   case "ISBN":
		   $searchCondition = " and Books.ISBN like '%".$searchText."%'";
		   break;
		   
		   case "Title":
		   $searchCondition = " and Books.Title like '%".$searchText."%'";
		   break;
		   
		   case "Author":
		    $searchCondition = " and Books.Author like '%".$searchText."%'";
		   break;
		   
		   case "Keyword":
		    $searchCondition = " and(Books.Title like '%".$searchText."%' ";
			$searchCondition .= " or Books.Author like '%".$searchText."%' ";
			$searchCondition .= " or Books.Course like '%".$searchText."%' ";
			$searchCondition .= " or Books.Edition like '%".$searchText."%'";
			$searchCondition .= " or Books.Professor like '%".$searchText."%')";
		   break;
		  
		}
		
		 $connection = mysql_connect($host,$user,$pass)
		or die ("Can't connect to server, try again later, please");

	mysql_select_db($database);
	
		
	$query = "select Books.BookId, Books.Title, Books.Author, Books.ISBN, Books.Edition, BookFormat.Description as BookFormat, Books.Course, Books.Professor, Books.Location, Books.Price, Books.PostDate
from Books, BookFormat
where Books.BookFormat = BookFormat.TypeId".$searchCondition." order by Books.PostDate desc;";

	$result = mysql_query($query,$connection)
		or die ("Problem with reading data from database, try again later.");
		
		$haveSearchResults = mysql_num_rows($result)>0;
		
		if (!$haveSearchResults){
		  echo "We're sorry,even though we don't have: \"".$searchText."\"  
		  <br/>
		 We know that campusbooks.com will have that book, so just click search now below: <br />"
		  ;
		  include("include/cbwidget.html");  	
		} else {
		echo "Search results for: ".$searchBy." \"".$searchText."\" (".mysql_num_rows($result)." matches)</br>";	
		}
		
		$oldDate = "";
		while( $row = mysql_fetch_row($result )){
			// get data from database
			$bookId = $row[0];
			$title = $row[1];
			$author = $row[2];
			$isbn = $row[3];
			$edition = $row[4];
			$bookType = $row[5];
			$course = $row[6];
			$professor = $row[7];
			$location = $row[8];
			$price = $row[9];
			$postDate = $row[10];
			
			// extract data in YYYY-MM-DD format
			$dateData = explode("-",$postDate);
			$year = $dateData[0];
			$month = $dateData[1];
			$day = $dateData[2];
			
			if (strlen($location)>1){
			  $location = " - (".$location.")";	
			} else {
			 $location = "";	
			}
			
			// converting to format "Wednesday, August 05, 2009"
			$labelData = date("l, F d, Y", mktime(0,0,0,$month, $day, $year));
			$bookLink = $title.",".$author.",".$edition." - ".$price.$location;
			
			// check if we must make a "Wednesday, August 05, 2009" label
			if ($oldDate != $postDate){
			 $oldDate = $postDate;
			 echo "<h4>".$labelData."</h4>";
			}
			
			echo "<a href='bookDetails.php?bookId=".$bookId."'> ".$bookLink."</a>";
		echo "<br/>";
		}
	
	} else {
		// show all books by default
		
	 echo "<h3> Recently Added books: </h3>";
		
	include("recentBooks.php");
	}


?>
    </div>
  <?php include("footer.php"); ?>
</div><!--mainfrm -->
   </div><!--frame-->
 </div><!--footerBg-->
</div><!--back -->
</body>
</html>
